﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace benefit.Controllers
{
    public class XingNengController : System.Web.Mvc.Controller
    {
        //
        // GET: /XingNeng/

        public ActionResult Index(int uid, string pdate)
        {

            Common.Web.Cookie cookie = new Common.Web.Cookie();
            ViewBag.datequery = cookie.GetCookie("querydate");
            ViewBag.pdate = cookie.GetCookie("querydate");
            int operId = Convert.ToInt32(cookie.GetCookie("LoginUserId"));
            if (BenefitHelper.Sys.VisitPrower.HasOperatorPrower(operId, uid, db))
            {
                if (pdate.Length > 4)//查询月数据
                {

                    ViewBag.queryyear = pdate.Substring(0, 4);
                    ViewBag.querymonth = pdate.Substring(4, 2);
                    BenefitHelper.Sys.Operator op = db.Operators.Find(uid);
                    ViewBag.op = op;
                    ViewBag.queryuser = uid;
                    int year = Convert.ToInt32(pdate.Substring(0, 4));
                    int month = Convert.ToInt32(pdate.Substring(4, 2));
                    BenefitHelper.View.OperatorMonthSum oms = new BenefitHelper.View.OperatorMonthSum().GetOperatorMonthSum(uid, month, year, db);
                    if (oms == null)
                    {
                        return RedirectToAction("Index/" + uid + "/" + pdate, "Error", null);
                    }
                    ViewBag.operatormonthsum = oms;
                    int lastyear = year;
                    int lastmonth = month;
                    if (month == 1)
                    {
                        lastmonth = 12;
                        lastyear = lastyear - 1;
                    }
                    else
                    {
                        lastmonth = lastmonth - 1;
                    }
                    BenefitHelper.View.OperatorMonthSum yoms = new BenefitHelper.View.OperatorMonthSum().GetOperatorMonthSum(uid, lastmonth, lastyear, db);
                    if (yoms != null)
                    {
                        ViewBag.yoperatormonthsum = yoms;
                    }
                    else
                    {
                        ViewBag.yoperatormonthsum = new BenefitHelper.View.OperatorMonthSum();
                    }
                    int qunian = year - 1;
                    BenefitHelper.View.OperatorMonthSum quniantongqi = new BenefitHelper.View.OperatorMonthSum().GetOperatorMonthSum(uid, month, qunian, db);
                    if (quniantongqi != null)
                    {
                        ViewBag.quniantongqi = quniantongqi;
                    }
                    else
                    {
                        ViewBag.quniantongqi = new BenefitHelper.View.OperatorMonthSum();
                    }


                    BenefitHelper.Sys.TradeHistory th = new BenefitHelper.Sys.TradeHistory();
                    string lastdate = th.GetLastTradeHistoryDateString(db);
                    ViewBag.lastdate = lastdate;
                    ViewBag.queryuser = uid;
                    string js = BenefitHelper.Tools.ChartAtManager.ShouYiBiLVLine(uid, month, year, db);
                    //    js += BenefitHelper.Tools.ChartAtManager.YearPie(month, year, uid, db);
                    js += BenefitHelper.Tools.ChartAtManager.GetOperatorAllBenefitPie(uid, month, year, true, db);
                    js += BenefitHelper.Tools.ChartAtManager.GetOperatorAllBenefitPie(uid, month, year, false, db);
                    //   js += BenefitHelper.Tools.ChartAtManager.MonthBillCount(month, year, uid, db);
                    ViewBag.js = js;
                }
                else
                {
                    ViewBag.queryyear = pdate.Substring(0, 4);
                    ViewBag.querymonth = "";
                    BenefitHelper.Sys.Operator op = db.Operators.Find(uid);
                    ViewBag.op = op;
                    ViewBag.queryuser = uid;
                    int year = Convert.ToInt32(pdate.Substring(0, 4));
                    //今年值
                    BenefitHelper.View.OperatorYearSum oms = new BenefitHelper.View.OperatorYearSum().GetOperatorYearSum(uid, year, db);
                    ViewBag.operatormonthsum = oms;
                    int lastyear = year;






                    
                    //BenefitHelper.View.OperatorYearSum yoms = new BenefitHelper.View.OperatorYearSum().GetOperatorYearSum(uid, year - 1, db);
                    //if (yoms != null)
                    //{
                    //    ViewBag.yoperatormonthsum = yoms;
                    //}
                    //else
                    //{
                    //    ViewBag.yoperatormonthsum = new BenefitHelper.View.OperatorYearSum();
                    //}
                    //去年值
                    BenefitHelper.View.OperatorYearSum quniantongqi = new BenefitHelper.View.OperatorYearSum().GetOperatorYearSum(uid, year - 1, db);
                    if (quniantongqi != null)
                    {
                        ViewBag.quniantongqi = quniantongqi;
                    }
                    else
                    {
                        ViewBag.quniantongqi = new BenefitHelper.View.OperatorYearSum();
                    }


                    string js = BenefitHelper.Tools.ChartAtManager.ShouYiBiLVYearLine(uid, year, db);
                    //    js += BenefitHelper.Tools.ChartAtManager.YearPie(month, year, uid, db);
                    js += BenefitHelper.Tools.ChartAtManager.GetOperatorYearAllBenefitPie(uid, year, true, db);
                    js += BenefitHelper.Tools.ChartAtManager.GetOperatorYearAllBenefitPie(uid, year, false, db);
                    //   js += BenefitHelper.Tools.ChartAtManager.MonthBillCount(month, year, uid, db);
                    ViewBag.js = js;

                }
            }
            else
            {
                db.Dispose();
                return Redirect("/Error/HasNoPrower/" + uid + "/" + pdate);
            }

            return View();

        }

    }
}
